/**
 * 测试 Weekly Energy Target API 中的 TEF 值
 */

const API_BASE_URL = 'http://localhost:3000/api/health/weekly-energy-target'

// 测试数据
const TEST_USER_ID = 68 // 使用示例数据中的用户ID

/**
 * 测试获取一周能量目标（包含TEF值）
 */
async function testWeeklyEnergyTargetWithTEF() {
  console.log('\n=== 测试一周能量目标API（验证TEF值） ===')
  
  const testCases = [
    {
      goal: 'maintain',
      rate: '0.5',
      description: '维持体重'
    },
    {
      goal: 'lose',
      rate: '0.5',
      description: '减重 0.5kg/周'
    },
    {
      goal: 'gain',
      rate: '0.25',
      description: '增重 0.25kg/周'
    }
  ]
  
  for (const testCase of testCases) {
    console.log(`\n--- 测试场景: ${testCase.description} ---`)
    
    try {
      const today = new Date()
      const dateStr = today.toISOString().split('T')[0]
      
      const url = `${API_BASE_URL}?userId=${TEST_USER_ID}&date=${dateStr}&goal=${testCase.goal}&rate=${testCase.rate}`
      console.log('请求URL:', url)
      
      const response = await fetch(url)
      const result = await response.json()
      
      console.log('响应状态:', response.status)
      
      if (result.success && result.data) {
        const data = result.data
        console.log('能量计算结果:')
        console.log(`  BMR (基础代谢率): ${data.bmr} kcal`)
        console.log(`  TEF (食物热效应): ${data.tef} kcal`)
        console.log(`  Historical PAL (活动水平系数): ${data.historical_pal}`)
        console.log(`  维持体重热量: ${data.maintenance_calories} kcal`)
        console.log(`  目标TEE: ${data.target_tee} kcal`)
        console.log(`  建议摄入: ${data.target_intake} kcal`)
        console.log(`  每日缺口/盈余: ${data.daily_deficit_surplus} kcal`)
        
        // 验证TEF计算是否合理
        const expectedTEF = (data.bmr + (data.historical_pal * data.bmr)) * 0.1
        const calculatedTEF = data.tef
        const tefDifference = Math.abs(expectedTEF - calculatedTEF)
        
        console.log(`\nTEF验证:`)
        console.log(`  期望TEF: ${expectedTEF.toFixed(1)} kcal`)
        console.log(`  实际TEF: ${calculatedTEF} kcal`)
        console.log(`  差异: ${tefDifference.toFixed(1)} kcal`)
        
        if (tefDifference < 1) {
          console.log('  ✅ TEF计算正确')
        } else {
          console.log('  ❌ TEF计算可能有误')
        }
        
      } else {
        console.log('请求失败:', result.error || result.message)
      }
      
    } catch (error) {
      console.error(`测试 ${testCase.description} 失败:`, error)
    }
  }
}

/**
 * 主测试函数
 */
async function main() {
  console.log('开始测试 Weekly Energy Target API 的 TEF 值...')
  console.log('测试用户ID:', TEST_USER_ID)
  
  await testWeeklyEnergyTargetWithTEF()
  
  console.log('\n测试完成!')
}

// 运行测试
if (require.main === module) {
  main().catch(console.error)
}

module.exports = {
  testWeeklyEnergyTargetWithTEF
}
